package com.renchunlin.sectetdemo;

import android.util.Log;

import com.fanjun.messagecenter.MessageCenter;
import com.fanjun.messagecenter.Msg;
import com.fanjun.messagecenter.socket.ConnetState;
import com.fanjun.messagecenter.socket.SocketInterceptor;

/*
 * class title:
 * Author by RenChunLin, Email 18957806320@163.com, Date on 2021/5/28.
 * PS: Not easy to write code, please indicate.
 */
public class MySocketInterceptor extends SocketInterceptor {

    /**
     * 分发服务器派发的数据包，注意派发不同的TAG给不同的订阅者
     *
     * @param msg 收到的数据包
     */
    @Override
    public void receiveServerMsg(String msg) {
        //发送应用内消息
        Msg insideMsg = Msg.ini("getMessage", msg);//"insideTag"是订阅消息的标识，new Object()是携带的数据包，可以是任意类型
        insideMsg.destination(Msg.APP_INSIDE);//APP_INSIDE为默认值，可以不用设置，如需发送到服务器则使用APP_OUTSIDE
        MessageCenter.sendMessage(insideMsg);
    }

    /**
     * 把发送socket的对象包装成服务器期望的字符串
     *
     * @param msgObj 发送socket的obj对象
     * @return 最终发送的socket的字符串
     */
    @Override
    public String packageMsg(Object msgObj) {
        return msgObj.toString();
    }

    @Override
    public void connectState(ConnetState connetState, Exception e) {
        super.connectState(connetState, e);
        if (connetState == ConnetState.SUCCESS) {
            Log.i("chen", "连接成功: ");
        } else if (connetState == ConnetState.INTERRUPT) {
            Log.i("chen", "连接已断开: ");
        }
    }
}
